package com.ltxtiyu.video.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ltxtiyu.video.common.ConfigConstant;
import com.ltxtiyu.video.entity.Music;
import com.ltxtiyu.video.entity.Topic;
import com.ltxtiyu.video.service.ms.ConfigService;
import com.ltxtiyu.video.service.ms.MusicService;
import com.ltxtiyu.video.service.ms.TopicService;
import com.ltxtiyu.video.tool.AliTool;
import com.ltxtiyu.video.tool.FileTool;
import com.ltxtiyu.video.tool.StringUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("musicController")
public class MusicController {

    @Reference
    private MusicService musicService;
    @Reference
    private ConfigService configService;

    /**
     * 条件查询列表
     * @param page
     * @param limit
     * @param title
     * @param artist
     * @return
     */
    @RequestMapping(value = "paramlist",method = RequestMethod.GET)
    public ResponseEntity<Map<String ,Object>> queryById(int page,int limit, String title,String  artist){
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("page",(page-1)*limit);
            map.put("row",limit);
            if(StringUtil.isNotBlank(title)){

                map.put("title",title);
            }
            if(StringUtil.isNotBlank(artist)){

                map.put("artist",artist);
            }
            Map<String,Object> result = musicService.selectParamList(map);

            return ResponseEntity.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }
    @PutMapping("/music")
    public ResponseEntity addMusic(Music music){
        try {

                musicService.updateMusic(music);
                return  new  ResponseEntity(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    @PostMapping("/music")
    public ResponseEntity updateMusic(Music music){
        try {
            musicService.addMusic(music);

            return new ResponseEntity(HttpStatus.CREATED);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
    }


    //更新与新增
    @RequestMapping(value = "addAndUpdate")
    public  ResponseEntity addAndUpdate(Music music){
        try {
            Map<String,Object> map = new HashMap<>();
            if(music.getId() == null){
                map.put("code",1);
                musicService.addMusic(music);
                return new ResponseEntity(map,HttpStatus.CREATED);
            }else{
                map.put("code",2);
                musicService.updateMusic(music);
                return  new  ResponseEntity(map,HttpStatus.NO_CONTENT);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);

    }



    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("delByIds")
    @ResponseBody
    public  String delByIds(String ids){
        return musicService.delByIds(ids);
    }

    /**
     *  上传bgm
     * @param file
     * @return
     */
    @RequestMapping("upload")
    public ResponseEntity<Map<String,Object>> upload(MultipartFile file){
        try {
            String OSS_ENDPOINT = configService.selectByCkey(ConfigConstant.OSS_ENDPOINT).getCvalue();
            String ACCESS_ID = configService.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
            String ACCESS_KEY = configService.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
            String BUCKET_ID = configService.selectByCkey(ConfigConstant.BUCKET_ID_MUSIC).getCvalue();
            String OSS_CDN = configService.selectByCkey(ConfigConstant.OSS_CDN_MUSIC).getCvalue();
            // 上传用OSS_ENDPOINT
            AliTool aliTool = AliTool.getInstance(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
            // 查看用OSS_CDN
            AliTool showAliTool = AliTool.getInstance(OSS_CDN, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
            InputStream inputStream = file.getInputStream();
            String aliyunId = aliTool.uploadFileByStreamForMusic(inputStream);
            Map<String,Object> result = new HashMap<>();
            result.put("aliyunId",aliyunId);
            result.put("url",showAliTool.getAccessURL(aliyunId));
            return ResponseEntity.ok(result);
        }catch (Exception e){
            return  ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }

    }

    /**
     * 播放
     * @param musicId
     * @return
     */
    @RequestMapping("openBgm")
    public ResponseEntity openBgm(String musicId){
        try {
            String OSS_ENDPOINT = configService.selectByCkey(ConfigConstant.OSS_ENDPOINT).getCvalue();
            String ACCESS_ID = configService.selectByCkey(ConfigConstant.ACCESS_ID).getCvalue();
            String ACCESS_KEY = configService.selectByCkey(ConfigConstant.ACCESS_KEY).getCvalue();
            String BUCKET_ID = configService.selectByCkey(ConfigConstant.BUCKET_ID_MUSIC).getCvalue();
            String OSS_CDN = configService.selectByCkey(ConfigConstant.OSS_CDN_MUSIC).getCvalue();
            // 查看用OSS_CDN
            AliTool showAliTool = AliTool.getInstance(OSS_CDN, ACCESS_ID, ACCESS_KEY, BUCKET_ID);
            String accessURL = showAliTool.getAccessURL(musicId);
            Map<String,Object> result = new HashMap<>();
            result.put("url",accessURL);
            return new ResponseEntity(result,HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
